package com.express.dao.impl;

import com.express.bean.MapArea;
import com.express.dao.BaseAdminDao;
import com.express.util.DruidUtil;

import java.sql.*;
import java.util.*;
import java.util.Date;

/**
 * @Author: laixiangdong
 * @Date 2020/12/03
 */
public class AdminDaoMysql implements BaseAdminDao {
    private static final String SQL_UPDATE_LOGIN_TIME = "UPDATE eadmin SET logintime = ?, loginip = ? WHERE username = ?";
    private static final String SQL_LOGIN = "SELECT id FROM eadmin WHERE username = ? AND password = ?";
    private static final String SQL_FIND_SYS_PHONE = "SELECT userphone FROM eadmin WHERE username = ?";
    private static final String SQL_CONSOLE = "SELECT COUNT(id) eadminDataSize, COUNT(TO_DAYS( createtime )= TO_DAYS( NOW())) eadminDataDay FROM eadmin";
    private static final String SQL_FIND_ALL_AREA = "SELECT area,COUNT(area) num FROM express GROUP BY area;";

    /**
     * 更新用户登陆的时间与IP
     *
     * @param username
     * @param date
     * @param ip
     */
    @Override
    public void updateLoginTime(String username, Date date, String ip) {
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(SQL_UPDATE_LOGIN_TIME);
            preparedStatement.setDate(1, new java.sql.Date(date.getTime()));
            preparedStatement.setString(2, ip);
            preparedStatement.setString(3, username);
            preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtil.close(connection, preparedStatement, null);
        }
    }

    /**
     * 管理员登陆
     *
     * @param username
     * @param password
     * @return 登陆的结果，true 表示登陆成功
     */
    @Override
    public boolean login(String username, String password) {
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(SQL_LOGIN);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            resultSet = preparedStatement.executeQuery();
            return resultSet.next();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtil.close(connection, preparedStatement, resultSet);
        }
        return false;
    }

    /**
     * 通过管理员名称查询管理员手机号码
     *
     * @param userName 管理员用户名
     * @return
     */
    @Override
    public String findSysPhone(String userName) {
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(SQL_FIND_SYS_PHONE);
            preparedStatement.setString(1, userName);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                String sysPhone = resultSet.getString("userphone");
                return sysPhone;
            }
        } catch (SQLIntegrityConstraintViolationException e) {
            System.out.println("导入数据重复了");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtil.close(connection, preparedStatement, resultSet);
        }
        return null;
    }

    @Override
    public Map<String, Integer> console() {
        Map<String, Integer> data = new HashMap<>();
        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        try {
            state = conn.prepareStatement(SQL_CONSOLE);
            result = state.executeQuery();
            if (result.next()) {
                int eadminDataSize = result.getInt("eadminDataSize");
                int eadminDataDay = result.getInt("eadminDataDay");
                data.put("eadminDataSize", eadminDataSize);
                data.put("eadminDataDay", eadminDataDay);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtil.close(conn, state, result);
        }
        return data;
    }

    @Override
    public List<MapArea> map() {
        List<MapArea> datas = new ArrayList<>();

        Connection conn = DruidUtil.getConnection();
        PreparedStatement state = null;
        ResultSet result = null;
        try {
            state = conn.prepareStatement(SQL_FIND_ALL_AREA);
            result = state.executeQuery();
            while (result.next()) {
                MapArea data = new MapArea();
                String area = result.getString("area");
                int num = result.getInt("num");
                data.setName(area);
                data.setValues(num);
                datas.add(data);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DruidUtil.close(conn, state, result);
        }
        return datas;
    }
}
